; DISPLAY START SPLASH SCREEN
multiview splashscreen.png windowtop 100 windowleft 100

; CHECK IF A PREVIOUS VERSION OF JACK IS INSTALLED
if not exists APPDIR:Jack
	requestchoice title "Jack Installer" body "Please note: The JackTunes Extension requires Python 2.5.5 in order to operate correctly." gadgets "Install Jack" >nil:
	requestfile Title "Where do you wish to install Jack? (A drawer will be created)" drawersonly noicons >ENV:amidock_destination
else
	requestfile Title "UPGRADE: Select the root drawer where Jack is installed" drawersonly noicons >ENV:amidock_destination
	if exists "`pathpart $amidock_destination`jack/jack.xml"
		delete "`pathpart $amidock_destination`jack/jack.xml"
	endif
endif

; INSTALL THIRD-PARTY DEPENDANCIES
if exists ENV:amidock_destination

	if not exists SYS:C/lha
		copy utilities/AmigaOS/lha C: quiet
	endif

	copy Distribution/#? "$amidock_destination" ALL FORCE QUIET

	Copy Executables/AmigaOS/#? "`pathpart $amidock_destination`Jack/" ALL FORCE QUIET

	; ENSURE SMBFS IS INSTALLED FOR THE SAMBA MUSIC PLAYER
	if exists C:smbfs
		version >nil: c:smbfs 1 74
		if warn
			copy utilities/AmigaOS/smbfs c: QUIET
		endif
	else
		copy utilities/AmigaOS/smbfs c: QUIET
	endif

	; WE NEED THIS SO THAT JACK CAN CHECK THE AMIDOCK IN ORDER TO ADD APPLICATION ICON INTO THE MAIN DOCK
	if exists c:arexxxml
		version >nil: c:arexxxml 1 1
		if warn
			copy utilities/AmigaOS/arexxxml c: QUIET
		endif
	else
		copy utilities/AmigaOS/arexxxml c: QUIET
	endif

	; REQUIRED BY AREXXXML
	if not exists sobjs:libexpat.so
		copy sobjs/libexpat.so sobjs: QUIET
	endif

	; REQUIRED BY AREXXXML
	if exists libs:expat.library
		version >nil: libs:expat.library 53 3
		if warn
			copy libs/AmigaOS/expat.library libs: QUIET
		endif
	else
		copy libs/AmigaOS/expat.library libs: QUIET
	endif

	; REQUIRED BY AREXXXML
	if exists libs:uni.library
		version >nil: libs:uni.library 5 12
		if warn
			copy libs/AmigaOS/uni.library libs: QUIET
		endif
	else
		copy libs/AmigaOS/uni.library libs: QUIET
	endif

	; WE NEED THIS SO THAT JACK CAN HAVE A WEATHER APP-ICON
	if exists c:image2icon
		version >nil: c:image2icon 1 23
		if warn
			copy utilities/AmigaOS/image2icon c:QUIET
		endif
	else
		copy utilities/AmigaOS/image2icon c: QUIET
	endif

	; ALWAYS COPY WEBP TO THE UTILITIES DRAWER, because it currently doesn't have a version number
	if not exists c:cwebp
		copy utilities/AmigaOS/cwebp c: QUIET
	endif

	; INSTALL THIRD-PARTY WEBP DATATYPE TO EXPAND THE USE OF THIS DATATYPE IN THE IMAGE EDITOR
	if not exists devs:datatypes/WebP
		copy datatypes/WebP#? devs:datatypes/
	endif

	; ASSOCIATED LIBRARY FOR WEBP
	if exists libs:webp.library
		version >nil: libs:AmigaOS/WebP.library 1 1
		if warn
			copy libs/AmigaOS/WebP.library libs: QUIET
		endif
	else
		copy libs/AmigaOS/WebP.library libs: QUIET
	endif

	; ALWAYS COPY OPENURL TO C DRAWER, because OpenUrl version string is invalid
	if not exists c:openurl
		copy utilities/AmigaOS/openurl c: QUIET
	endif

	; PREFERENCES TOOL REQUIRED BY OPENURL
	if not exists "SYS:Prefs/OpenURL"
		copy "utilities/AmigaOS/Prefs/OpenURL" SYS:Prefs/ QUIET
		copy "utilities/AmigaOS/Prefs/OpenURL.info" SYS:Prefs/ QUIET
	endif

	; LIBRARY FOR OPENURL
	if not exists libs:openurl.library
		copy libs/AmigaOS/openurl.library libs: QUIET
	endif

	; DEFAULT OPENURL PREFS FILE, PERFECT SETTINGS FOR OWB AND NETSURF
	if not exists envarc:OpenURL.prefs
		copy utilities/AmigaOS/prefs/OpenURL.prefs envarc: QUIET
		copy utilities/AmigaOS/prefs/OpenURL.prefs env: QUIET
	endif

endif

; IF USER HAS USED JACK 1.8 IN THE PAST, REMOVE THIS REDUNDANT FILE
if exists ENVARC:jack.xml
	delete envarc:jack.xml QUIET
endif

; CREATE HOLLYWOOD PLUG-INS DRAWER IF REQUIRED
if not exists libs:hollywood
	makedir libs:Hollywood
endif

; INSTALL HOLLYWOOD PLUG-INS WHERE REQUIRED
if exists libs:Hollywood
	if exists libs:hollywood/avcodec.hwp
		version >nil: libs:hollywood/avcodec.hwp 1
		if warn
			copy plug-ins/AmigaOS/avcodec.hwp libs:hollywood/ FORCE QUIET
		endif
	else
		copy plug-ins/AmigaOS/avcodec.hwp libs:hollywood/ FORCE QUIET
	endif
	if exists libs:hollywood/svgimage.hwp
		version >nil: libs:hollywood/svgimage.hwp 1
		if warn
			copy plug-ins/AmigaOS/svgimage.hwp libs:hollywood/ FORCE QUIET
		endif
	else
		copy plug-ins/AmigaOS/svgimage.hwp libs:hollywood/ FORCE QUIET
	endif
	if exists libs:hollywood/vectorgraphics.hwp
		version >nil: libs:hollywood/vectorgraphics.hwp 1
		if warn
			copy plug-ins/AmigaOS/vectorgraphics.hwp libs:hollywood/ FORCE QUIET
		endif
	else
		copy plug-ins/AmigaOS/vectorgraphics.hwp libs:hollywood/ FORCE QUIET
	endif
	if exists libs:hollywood/oggtheora.hwp
		version >nil: libs:hollywood/oggtheora.hwp 1
		if warn
			copy plug-ins/AmigaOS/oggtheora.hwp libs:hollywood/ FORCE QUIET
		endif
	else
		copy plug-ins/AmigaOS/oggtheora.hwp libs:hollywood/ FORCE QUIET
	endif
	if exists libs:hollywood/oggvorbis.hwp
		version >nil: libs:hollywood/oggvorbis.hwp 1
		if warn
			copy plug-ins/AmigaOS/oggvorbis.hwp libs:hollywood/ FORCE QUIET
		endif
	else
		copy plug-ins/AmigaOS/oggvorbis.hwp libs:hollywood/ FORCE QUIET
	endif
	if exists libs:hollywood/moviesetter.hwp
		version >nil: libs:hollywood/moviesetter.hwp 1 1
		if warn
			copy plug-ins/AmigaOS/moviesetter.hwp libs:hollywood/ FORCE QUIET
		endif
	else
		copy plug-ins/AmigaOS/moviesetter.hwp libs:hollywood/ FORCE QUIET
	endif
endif

; INSTALL PYTHON DEPENDANCIES FOR THE SAMBA MUSIC PLAYER
if exists Python:
	if not exists Python:Lib
		MakeDir Python:lib
		MakeDir Python:lib/site-packages
	endif

	if not exists Python:Scripts
		MakeDir Python:Scripts
	endif

	copy python/lib/site-packages/#? Python:Lib/site-packages/ ALL FORCE QUIET
	copy python/scripts/#? Python:Scripts/ ALL FORCE QUIET
else
	requestchoice title "Jack Installer" body "Python was not found on your system.  Jack has been installed, but JackTunes will not work." gadgets "OK" >nil:
endif

wait 3

; ENSURE THE APPLICATION 'MPLAYER' HAS BEEN LAUNCHED AT LEAST ONCE IN THE HISTORY OF THE COMPUTER
if not exists APPDIR:Mplayer
	if exists SYS:C/Mplayer
		SYS:C/Mplayer >nil: *>nil:
	endif
endif

; ENSURE THE 'FIND' APPLICATION HAS BEEN LAUNCHED AT LEAST ONCE IN THE HISTORY OF THE COMPUTER
if not exists APPDIR:Find
	if exists SYS:System/Find
		find ram: >nil: *>nil:
	endif
endif

; CHECK IF ENVARC DRAWER FOR JACK EXISTS IF NOT CREATE IT
if not exists ENVARC:Jack
	makedir ENVARC:Jack
endif

; ASK USER WHETHER TO ADD TO WBSTARTUP
requestchoice title "Jack for AmigaOS" body "Do you wish to have have Jack start every time you boot up your computer?" gadgets "Yes (Recommended)" "No" >env:jack_to_wbstartup

if $jack_to_wbstartup eq 1
	wbstartupctrl ADD "`pathpart $amidock_destination`jack/Jack" >nil:
endif

; ASK USER WHETHER TO ADD TO AMIDOCK
requestchoice title "Jack for AmigaOS" body "Do you wish to have Jack added to your Dock?" gadgets "Yes" "No, thanks" >env:jack_to_amidock
if $jack_to_amidock eq 1
	; ATTEMPT TO ADD JACK TO THE AMIDOCK IF NOT PRESENT ON THE MAIN DOCK
	rx Installer.rexx
endif

; DISPLAY END SPLASH SCREEN
run >nil: multiview endsplash.png windowtop 100 windowleft 100

; IF INSTALLED FOR THE FIRST TIME FIND WHERE IT WAS INSTALLED TO SO WE CAN LAUNCH IT AFTERWARDS
if not exists APPDIR:Jack
	find dirs $jack_destination name jack nodates nosizes exactname >env:jackinstall
	echo $jackinstall >envarc:appdir/Jack
endif

; CLEAN-UP
delete env:amidock_destination FORCE QUIET
delete env:jack_to_wbstartup FORCE QUIET
delete env:jack_to_amidock FORCE QUIET

; ATTEMPT TO RUN JACK AFTER INSTALLATION
run >nil: *>nil: APPDIR:Jack